<?php
namespace app\console\controller;
use app\console\controller\Auth;
use think\facade\Config;
use think\Db;
use app\service\controller\File;

/**
 * 首页设置
 * */
class About extends Auth
{
    // pc端
    public function about_list(){
        $lists=Db::table('tp_about')
            ->paginate(10);

        $this->assign('active', 'about');
        $this->assign([
            'topbar' => ['关于我们设置', '公司介绍设置'],
            'seting' => Config::pull('cms_config'),
            'lists'  =>$lists
        ]);
        return $this->fetch();
    }

    //关于我们添加
    public function about_add(){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_about')->insert($data);
            $this->success('新增成功');
            exit;
        }
        $this->assign([
            'topbar' => ['关于我们设置', '公司介绍设置','添加'],
        ]);
        return $this->fetch();
    }
    //关于我们修改
    public function about_edit($id){
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            //上传图片
            $cate_img = (new \app\service\controller\File())->upload_single_img('head_img', 'head_img');
            if (is_array($cate_img)) {
                if ($cate_img['code'] == 1) {
                    $data['head_img'] = $cate_img['img_path'];
                } else {
                    $this->error($cate_img['msg']);
                }
            } else {
                unset($data['head_img']);
            }
            //更新栏目
            $res=Db::table('tp_about')->where('id',$id)->update($data);
            $this->success('修改成功');
            exit;
        }
        $res=Db::table('tp_about')->where('id',$id)->find();
        $this->assign([
            'topbar' => ['关于我们设置', '公司介绍设置','修改'],
            'seting' => Config::pull('cms_config'),
            'list'   =>$res
        ]);
        return $this->fetch();
    }

}
